/*
 * Copyright (C) 2015 MC
 * All rights reserved.
 *
 * $$File: $$
 * $$DateTime: $$
 * $$Author: $$
 * $$Revision: $$
 */
 
package com.glela.common.verify;

import javax.validation.ConstraintValidator;  
import javax.validation.ConstraintValidatorContext;  
import java.lang.reflect.Method;
/**
 * @author liujie
 *
 *	重写验证规则，使用自定义验证方法
 */
public class CustomValidator implements ConstraintValidator<Custom,Object>{  
    private String method;  
    private Class<?> clazz;  
  
    @Override
	public void initialize(Custom custom) {  
        this.method = custom.method();  
        this.clazz = custom.clazz();
    }  
  
    @Override
	public boolean isValid(Object value, ConstraintValidatorContext context) { 
    	boolean result = false;
    	try {
    		
			for (Method m : clazz.getMethods()) {
				if(m.getName().equals(method)) {
					result = (boolean) m.invoke(clazz, value);
					break;
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
        return result;  
    }  
}  
